/*
 * Copyright (c) 2019 DawnFramework. All Rights Reserved.
 * Licensed under the MIT (LICENSE) license.
 */

package org.dawn.common.service.service;

import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @author Willard Hu on 2017/4/25.
 */
public class ServiceTSingleResponse<T> extends ServiceResponse {
    private static final long serialVersionUID = 6057532242651260166L;

    private T value;

    public void setValue(T value) {
        this.value = value;
    }

    public T get() {
        return value;
    }

    public boolean isPresent() {
        return value != null;
    }

    public void ifPresent(Consumer<T> consumer) {
        if (value != null) {
            consumer.accept(value);
        }
    }

    public T orElse(T other) {
        return value != null ? value : other;
    }

    public T orElseGet(Supplier<T> other) {
        return value != null ? value : other.get();
    }
}
